package com.manman.eduService.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manman.eduService.entity.EduCourse;
import com.manman.eduService.entity.EduCourseDescription;
import com.manman.eduService.entity.vo.CourseInfoVo;
import com.manman.eduService.entity.vo.CoursePublishVo;
import com.manman.eduService.mapper.EduCourseMapper;
import com.manman.eduService.service.EduChapterService;
import com.manman.eduService.service.EduCourseDescriptionService;
import com.manman.eduService.service.EduCourseService;
import com.manman.eduService.service.EduVideoService;
import com.manman.serviceBase.exception.GuException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-31
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    EduCourseDescriptionService courseDescriptionService;

    // 小节
    @Autowired
    EduVideoService eduVideoService;

    // 章节
    @Autowired
    EduChapterService eduChapterService;

    // 添加课程基本信息
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        // 1. 向课程表添加课程信息
        // CourseInfO 对象转为 eduCourse 对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert <= 0) {
            throw new GuException(20001, "添加课程信息失败");
        }

        // 2. 向课程简介表添加课程简介
        String cid = eduCourse.getId();
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(cid);
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.save(courseDescription);
        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        // 1. 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        // 2. 查询描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        // 1. 修改课程表
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, course);
        int update = baseMapper.updateById(course);
        if (update == 0) {
            throw new GuException(20001, "修改课程信息失败");
        }
        // 2. 修改描述表
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, description);
        courseDescriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public void removeCourse(String courseId) {
        // 1. 根据课程 id 删除小节
        eduVideoService.removeByCourseId(courseId);

        // 2. 根据课程 id 删除章节
        eduChapterService.removeByCourseId(courseId);

        // 3. 根据课程 id 删除课程描述
        courseDescriptionService.removeById(courseId);

        // 4. 根据课程 id 删除课程信息
        int result = baseMapper.deleteById(courseId);

        if (result == 0) {
            throw new GuException(20001, "删除失败");
        }
    }
}
